#!/bin/bash
set -e

readonly GIT_TOPDIR=$(git rev-parse --show-toplevel 2>/dev/null || :)

# Usage: $ NR_MOD=6 git commit -s
[[ -z ${NR_MOD} ]] && NR_MOD=4
[[ -z ${NR_ADD} ]] && NR_ADD=1
[[ -z ${NR_DEL} ]] && NR_DEL=1
[[ -z ${FORCE} ]] && FORCE=

if [[ ${V} ]] || [[ ${VERBOSE} ]]; then
	set -x
fi

try_add=( $(git status --short | grep ^A | awk '{print $2}' || true) )
try_del=( $(git status --short | grep ^D | awk '{print $2}' || true) )
try_modify=( $(git status --short | grep ^M | awk '{print $2}' || true) )

fatal() {
	echo >&2 "FATAL: "${@}
	exit 1
}

# Redirect output to stderr.
exec 1>&2

hint_changelog() {
	if [[ -f ${GIT_TOPDIR}/CHANGELOG.md ]] &&
	  ! [[ -L ${GIT_TOPDIR}/CHANGELOG.md ]]; then
		if ! [[ " ${try_modify[@]} " =~ " CHANGELOG.md " ]]; then
			git status --short
			fatal "${@}" "you forgot modify CHANGELOG!!! Try FORCE???"
		fi
	fi
	if [[ -f ${GIT_TOPDIR}/docs/en/CHANGELOG.md ]]; then
		if [[ ! " ${try_modify[@]} " =~ " docs/en/CHANGELOG.md " ]] ||
		   [[ ! " ${try_modify[@]} " =~ " docs/zh/CHANGELOG.md " ]]; then
			git status --short
			fatal "${@}" "you forgot modify docs/{en,zh}/CHANGELOG.md!!! Try FORCE???"
		fi
	fi
}

broken_symlinks=( $(find ${GIT_TOPDIR} -xtype l) )
if [[ ${#broken_symlinks[@]} -ge 1 ]]; then
	fatal "Broken-symlink: ${broken_symlinks[@]}"
fi

for f in ${try_add[@]}
do
	[[ -d ${f} ]] && continue

	# true: for empty file
	read -r -N 8 bin < $f || true
	if [[ $bin == $'\x7f'ELF* ]]; then
		fatal "You are trying to add <$f> ELF file to git-tree, see git status."
	# ar file start from "!<arch>\n", see /usr/include/ar.h
	elif [[ $bin == $'!<arch>\n' ]]; then
		fatal "You are trying to add <$f> ar file to git-tree, see git status."
	elif [[ $bin == $'gmon'* ]]; then
		fatal "You are trying to add <$f> GNU prof performance file to git-tree, see git status."
	fi
	if [[ $(basename $f) == vmlinux.h ]]; then
		fatal "vmlinux.h is too big, generate it with bpftool command!!!"
	fi
	if [[ $(file $f | awk '{print $NF}') == data ]] && [[ -z ${FORCE} ]]; then
		fatal "$f is considered a data file!!! try FORCE"
	fi
done

# modify,add,delete files, need modify CHANGELOG.md too
[[ ${#try_modify[@]} -ge ${NR_MOD} ]] && hint_changelog "<Modify too many files>"
[[ ${#try_del[@]} -ge ${NR_DEL} ]] && hint_changelog "<Delete files>"
[[ ${#try_add[@]} -ge ${NR_ADD} ]] && hint_changelog "<Add files: ${try_add[@]}>"

exit 0
